﻿$(function () {
    relogin();
    $(".button").live("click", function () {
        dopost();
    });
    $("input").keyup(function (event) {
        if (event.keyCode == 13) {
            dopost();
        }
    });
});
function relogin() {
    $.getJSON("UserHandler.ashx?action=relogin", {}, function (msg) {
        if (msg != null || msg != undefined) {
            if (msg.IsRight == "true") {
                window.location.href = "HealthMain.aspx";
            }
        }
    });
}
function dopost() {
    var name = $.trim($("input[name=username]").val());
    var password = $.trim($("input[name=password]").val());
    if (checkNull(name, password)) {
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "UserHandler.ashx",
            data: "login=" + name + "&password=" + password,
            success: function (msg) {
                if (msg.IsRight == "false") {
                    $("#errorbox").html("");
                    $("#errorbox").html(msg.ErrorMessage);
                }
                else {
                    window.location.href = "HealthMain.aspx";
                }
            },
            error: function () {
                $("#errorbox").html("");
                $("#errorbox").html(msg.ErrorMessage);
            },
            beforeSend: showStart()
        });
    }
}
function showStart() {
    $("#errorbox").html("");
    $("#errorbox").html("正在连接服务器...");
}
function checkNull(name, password) {
    if (name == "" || password == "") {
        $("#errorbox").html("账号密码不能为空!");
        return false;
    }
    else {
        return true;
    }
}